package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.chat.adapter.GroupSearchAdapter;
import com.hzcx.app.simplechat.ui.chat.bean.GroupSearchBean;
import com.hzcx.app.simplechat.ui.chat.contract.GroupSearchContract;
import com.hzcx.app.simplechat.ui.chat.presenter.GroupSearchPresenter;
import com.hzcx.app.simplechat.ui.group.AddGroupForSearchActivity;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GroupContactSearchActivity extends BaseActivity<GroupSearchPresenter> implements GroupSearchContract.View {

    public static final String INTENT_TAG = "INTENT_TAG";

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.rv_friend)
    RecyclerView rvFriend;

    private List<GroupSearchBean> groupSearchBeans;
    private GroupSearchAdapter searchAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_friend_search;
    }

    @Override
    public void initPresenter() {
        mPresenter = new GroupSearchPresenter();
    }

    @Override
    public void initView() {

        // 弹出软键盘
        AppHelper.showSoftKeyboard(etSearch, this);

        tvTips.setText("搜索群组号");
        groupSearchBeans = new ArrayList<>();
        searchAdapter = new GroupSearchAdapter(groupSearchBeans);
        rvFriend.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFriend.setAdapter(searchAdapter);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etSearch.getText().toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                    mPresenter.searchGroupList(GroupContactSearchActivity.this, etSearch.getText().toString());
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                tvTips.setVisibility(View.VISIBLE);
                groupSearchBeans.clear();
                searchAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    if (EmptyUtils.isEmpty(etSearch.getText().toString())) {
                        return false;
                    }
                    if (etSearch.getText().toString().length() > 0) {
                        ivClear.setVisibility(View.VISIBLE);
                        mPresenter.searchGroupList(GroupContactSearchActivity.this, etSearch.getText().toString());
                    } else {
                        ivClear.setVisibility(View.GONE);
                    }
                    tvTips.setVisibility(View.VISIBLE);
                    groupSearchBeans.clear();
                    searchAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        searchAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(this, AddGroupForSearchActivity.class);
            intent.putExtra(AddGroupForSearchActivity.bean, groupSearchBeans.get(position));
            startActivity(intent);
        });
    }

    /**
     * 群组搜索成功
     *
     * @param data
     */
    public void searchGroupListResult(List<GroupSearchBean> data) {

        Log.d(TAG, "searchGroupListResult: " + new Gson().toJson(data));
        if (data == null || data.size() <= 0) {
//            showError("未搜素到相关群组");
            return;
        }
        tvTips.setVisibility(View.GONE);
        groupSearchBeans.addAll(data);
        searchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail() {
//        LogUtils.d("搜索群组", new Gson().toJson("搜索群组失败"));
        Log.d(TAG, "onFail: " + new Gson().toJson("搜索群组失败"));
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}